from bs4 import BeautifulSoup
from classSVGNode import SVGNode
from classSVGEdge import SVGEdge
import pickle

def getSVG(id):
    path = 'svgdata/' + id + '_pruned.svg'
    dic = {'cluster':{}, 'edge':[]}
    file = open(path, 'r')
    content = file.read()
    file.close()
    soup=BeautifulSoup(content)
    for i in soup.find_all('g'):
        if i['class'] == [u'node']:
            node = SVGNode()
            node.id = i.find('title').get_text()
            node.x = i.find('ellipse')['cx']
            node.y = i.find('ellipse')['cy']
            dic['cluster'][node.id] = node
        elif i['class'] == [u'edge']:
            edge = SVGEdge()
            line = i.find('title').get_text()
            source = line.split('->')[0]
            target = line.split('->')[1]
            assist = i.find('path')['d']
            assist = assist.split('C')[1].split(' ')
            for i in range(len(assist)):
                assist[i] = assist[i].split(',')
            #print source,target,assist
            edge.source = source
            edge.target = target
            edge.assists = assist
            dic['edge'].append(edge)
    path = 'svgdata/result/'+id+'_svg.json'
    f = open(path, 'w')
    pickle.dump(dic, f)
    f.close()
